package tron

import (
	"encoding/json"
	"fmt"
	"raymond/global"
)

type TronResource int

const (
	BANDWIDTH TronResource = iota
	ENERGY
)

type TronResourceType int

const (
	_BANDWIDTH TronResourceType = iota
	_ENERGY
)

var tronResources = []string{"ENERGY", "BANDWIDTH"}

func (r TronResource) String() string {
	return tronResources[r]
}

func TestTransferTRC20(asset string, from string, to string, value float64) error {
	assetBal, err := GetTrxBalance(asset)
	if err != nil {
		return err
	}
	if assetBal > 10 {
		freezeUrl := GetTrcApiRpcUrl() + "/wallet/freezebalancev2"

		req := make(map[string]interface{})
		req["owner_address"] = asset
		req["frozen_balance"] = assetBal
		req["resource"] = tronResources[0]
		reqBytes, err := json.Marshal(req)
		if err != nil {
			return err
		}
		respBytes, err := global.HttpPost(freezeUrl, reqBytes)
		if err != nil {
			return err
		}
		fmt.Printf("%s \n", string(respBytes))
	}
	candelegateUrl := GetTrcApiRpcUrl() + "/wallet/getcandelegatedmaxsize"
	req := make(map[string]interface{})
	req["owner_address"] = asset
	req["type"] = _BANDWIDTH
	req["visible"] = true
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return err
	}
	respBytes, err := global.HttpPost(candelegateUrl, reqBytes)
	if err != nil {
		return err
	}
	fmt.Printf("%s \n", string(respBytes))

	return nil
}
